﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace MyPublisher.Framework
{
    public class LocalEntityFactory : EntityFactory
    {
        private TypesFactory typesFactory;

        private List<Package> packages;
        private List<File> files;
        private List<Content> documents;
        private List<Publishing> publishings;
        private List<Location> locations;
        private List<User> identies;
        private List<Connector> connectors;
        private Thread thread;

        public LocalEntityFactory(TypesFactory typesFactory)
        {
            this.typesFactory = typesFactory;

            this.packages = new List<Package>();
            this.files = new List<File>();
            this.documents = new List<Content>();
            this.publishings = new List<Publishing>();
            this.locations = new List<Location>();
            this.identies = new List<User>();
            this.connectors = new List<Connector>();

            InitTestData();

            this.thread = new Thread(SearchFiles);
            this.thread.Start();
        }

        public override IList<File> Files { get { return files; } }
        public override IList<Content> Documents { get { return documents; } }
        public override IList<Package> Packages { get { return packages; } }
        public override IList<Publishing> Publishings { get { return publishings; } }
        public override IList<Location> Locations { get { return locations; } }
        public override IList<User> Identities { get { return identies; } }
        public override IList<Connector> Connectors { get { return connectors; } }

        private User localIdentity;
        private User csdnBlogIdentity;
        private User msnBlogIdentity;
        private void InitTestData()
        {
            LocationType windowsType = typesFactory.LocationTypes[0];
            LocationType blogType = typesFactory.LocationTypes[1];

            localIdentity = new User() { ID = 1, Name = @"ca.com\zhach08", Password = "Woaixigua1234", Description = "Local System Identity" };
            csdnBlogIdentity = new User() { ID = 2, Name = "soudog", Password = "Woaixigua123", Description = "CSDN Blog Identity" };
            msnBlogIdentity = new User() { ID = 2, Name = "sou_dog", Password = "Woaixigua1234", Description = "MSN Space Identity" };

            this.identies.Add(localIdentity);
            this.identies.Add(csdnBlogIdentity);
            this.identies.Add(msnBlogIdentity);

            this.Locations.Add(
                new Location() 
                { Address = @"D:\book", Type = windowsType, Identity = localIdentity });

            this.Locations.Add(
                new Location() 
                { Address = @"C:\Users\zhach08\Desktop", Type = windowsType, Identity = localIdentity });

            //this.Locations.Add(
            //    new Location() { Address = @"http://blog.csdn.net/soudog/services/metablogapi.aspx ", Type = blogType, Identity = csdnBlogIdentity });
            this.Locations.Add(
                new Location() { Address = @"https://storage.msn.com/storageservice/MetaWeblog.rpc", Type = blogType, Identity = msnBlogIdentity });
        }

        private void SearchFiles()
        {
            foreach (Location location in Locations)
            {
                if (location == Locations[1] || !location.Type.Name.Equals("Windows"))
                    continue;

                string path = location.Address;
                string[] directories = 
                    System.IO.Directory.GetDirectories(path,"*.*",System.IO.SearchOption.AllDirectories);
                foreach (string directory in directories)
                {
                    System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(directory);
                    File file = new File() 
                    { 
                        Name = di.Name,
                        CreateDate = di.CreationTime,
                        LastModifyDate = di.LastAccessTime,
                        Description = di.Name,
                        Owner = location.Identity
                    };
                    this.files.Add(file);

                    string[] files = System.IO.Directory.GetFiles(directory);
                    foreach (string next in files)
                    {
                        System.IO.FileInfo fi = new System.IO.FileInfo(next);
                        string source = fi.FullName;
                        Content content = new Content(location, source)
                        {
                            Name = fi.Name,
                            CreateDate = fi.CreationTime,
                            LastModifyDate = fi.LastAccessTime,
                            Description = fi.Name,
                            Owner = location.Identity
                        };
                        file.Contents.Add(content);

                        documents.Add(content);

                        AddToPackage(content, fi);
                    }
                }
            }
        }

        private void AddToPackage(Content content, System.IO.FileInfo fi)
        {
            Dictionary<string, string> filters = new Dictionary<string, string>()
            {
                {".doc","Microsoft Word 97-2003"},
                {".docx","Microsoft Word 2007"},
                {".pptx","Microsoft Presentation 2007"},
                {".ppt","Microsoft Presentation 97-2003"},
                {".pdf","Adobe PDF"}
            };
            if (!filters.ContainsKey(fi.Extension))
                return;

            bool found = false;
            foreach (Package package in packages)
            {
                if (package.Kind == fi.Extension)
                {
                    package.Contents.Add(content);
                    found = true;
                    break;
                }
            }

            if (!found)
            {
                Package package = new Package() 
                { 
                    Kind = fi.Extension, 
                    Name = filters[fi.Extension],
                    Description = filters[fi.Extension]
                };
                this.packages.Add(package);
            }
        }
    }
}
